﻿using HandyControl.Data;
using Models;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Xml.Linq;

namespace SanmaoSystem.ViewModels
{
   public class CargoViewModel : BindableBase
    { 
        private string _tag="";
        public string Tag { get { return _tag; } set { SetProperty(ref _tag, value); } }
        private string _name="";
        public string Name { get { return _name; } set { SetProperty(ref _name, value); } }
        private string _id="";
        public string Id { get { return _id; } set { SetProperty(ref _id, value); } }
        private string _dataListShow = "Visible";
        public   string DataListShow { get { return _dataListShow; } set { SetProperty(ref _dataListShow, value); } }
         //编辑
        public DelegateCommand<object> Edit { get => new DelegateCommand<object>(OpenEdit); }
        //删除
        private DelegateCommand<int> _delete;
        public DelegateCommand<object> Delete { get => new DelegateCommand<object>(DeleteById);  }
         private readonly IRegionManager _regionManager;
        //模糊查询关键词
        private string _keyWord;
        public string KeyWord { get { return _keyWord; } set { SetProperty(ref _keyWord,value); } }
        //分页总页数
        private int _totalPage;
        public int TotalPage { get { return _totalPage; } set { SetProperty(ref _totalPage, value); } }
        //当前页码
        private int _pageIndex=1;
        //每页显示数据
        private int PageSize = 5;
          //换页命令
        public DelegateCommand<FunctionEventArgs<int>> PageUpdatedCmd { get => new DelegateCommand<FunctionEventArgs<int>>(Update); }
        public List<Cargo> list;
        public int PageIndex { get { return _pageIndex; } set { SetProperty(ref _pageIndex, value); } }
        //显示列表
        public ObservableCollection<Cargo> DataList { set; get; }
        //编辑/保存
        public DelegateCommand Save1 { get => new DelegateCommand(SaveEdit);  }
        //取消
        public DelegateCommand Cancel { get => new DelegateCommand(Cancel1);  }
           //添加
        public DelegateCommand Insert { get => new DelegateCommand(OpenInsertCargoType);  }
        public CargoViewModel() {

           DataList = new ObservableCollection<Cargo>();
            Reload();
        }
          public void OpenInsertCargoType()
        {
            Name = "";
            Tag = "";
            DataListShow = "Hidden";
        }
          private void Cancel1()
        {

             Name = "";
             Tag = "";
             Id = "";
             DataListShow = "Visible";
          

        }
          private void SaveEdit()

        {   CargoIProvider cargoIProvider = new CargoIProvider();
            Cargo cargo = new Cargo();
            if (Id == "")
            {
             cargo.Name = Name;
             cargo.Tag = Tag;
             cargo.InsertDate = DateTime.Now;
             cargo.MemberId = 0;
             cargo.MemberName = (string) Application.Current.Properties["UserName"];
             cargo.Price = 461;
             cargo.Unit = "12";
             cargo.TypeId = 66;
             cargo.TypeName = "牛逼";
             cargoIProvider.Insert(cargo);
            Name = "";
            Tag = "";
                MessageBox.Show("添加成功！");
            }
            else
            {
                cargo.Id = int.Parse(Id);
                cargo.Name = Name;
                cargo.Tag = Tag;
                cargoIProvider.Update(cargo);
                Name = "";
                Tag = "";
                 MessageBox.Show("修改成功！");
            }             
            DataListShow = "Visible";
            Refresh(cargoIProvider);
        }
         private void OpenEdit(object obj)
        {
           CargoIProvider cargoIProvider = new CargoIProvider();
            int id = (int)obj;
            Cargo cargoType = cargoIProvider.GetById(id);
            Name = cargoType.Name;
            Tag = cargoType.Tag;
            Id = cargoType.Id.ToString();
            DataListShow = "Hidden";
        }
         public void Reload()
        {
            CargoIProvider cargoIProvider = new CargoIProvider();
             list = cargoIProvider.GetAll();
            //总页数 = (总数据量 + 每页数据量 - 1) / 每页数据量
            TotalPage = (list.Count+PageSize-1)/ PageSize;
            int start=(PageIndex-1)* PageSize;
            int end= Math.Min(start+PageSize,list.Count);
            GetList(start, end);
        }
         public void GetList( int start,int end)
        {

            DataList.Clear();
            var ListNow = list.GetRange(start, end-start);
            foreach(var item in ListNow)
            {
                DataList.Add(item);
            }
        }
         public void Update(FunctionEventArgs<int> pageIndex )
        {  
            int start=(pageIndex.Info-1)*PageSize;
            int end= Math.Min(start+PageSize,list.Count);
            GetList(start, end);
        }
          private void DeleteById(object obj)
        {   
             int id = (int)obj;
             CargoIProvider cargoIProvider = new CargoIProvider();
             cargoIProvider.Delete(id);
             Refresh(cargoIProvider);
            //总页数 = (总数据量 + 每页数据量 - 1) / 每页数据量
         
        
          
        }
          //刷新数据
        public void Refresh(CargoIProvider cargoIProvider )
        {
            list = cargoIProvider.GetAll();
            PageIndex = 1;
            int start=(PageIndex-1)* PageSize;
            int end= Math.Min(start+PageSize,list.Count);
            GetList(start, end);
        }

    }
}
